var operation_type = "verification_code_login"; //操作类型（手机号码登录，密码登录，微信登录，注册）
var check_count_down = true; //判断验证码是否过期
var interval; //计时对象
var true_verification_code = ""; //保存验证码
var count_down = 30; //30秒
var whether_check_svg = false; //是否选中用户协议
var url_html_name = ""; //返回指定页面url
var url_parameter = ""; //跳转指定页面参数
var wechat_interval = null; //微信计时对象

$(document).ready(function() {
	get_user_mess_and_token();
	var url = location.search;
	var url_x_index = url.indexOf('/'); /* 斜杠下标 */
	if (url.indexOf('?') != -1) {
		url_html_name = url.substring(url.indexOf('?') + 1, (url_x_index != -1 ? url_x_index : url
			.length)); //获取页面名称 
		if (url_x_index != -1) { //有参数
			url_parameter = url.substring(url_x_index + 1) == "/" ? "" : url.substring(url_x_index +
				1);
		}
	}
});

/* 提交操作 */
function button_submit() {
	var user_phone = $("[name='user_phone']").val();
	var verification_code = $("[name='verification_code']").val();
	var user_password = $("[name='user_password']").val();
	if (operation_type == "verification_code_login") { // 验证码登录
		if (check_phone() && check_verification_code()) {
			send_ajax("http://127.0.0.1:8899/api/consumer/UserController/login", JSON.stringify({
				"user": {
					'user_phone': user_phone
				},
				"token": token
			}));
		}
	} else if (operation_type == "password_login") { // 密码登录
		if (check_phone() && check_user_password()) {
			send_ajax("http://127.0.0.1:8899/api/consumer/UserController/login", JSON.stringify({
				"user": {
					'user_phone': user_phone,
					'user_password': user_password
				},
				"token": token
			}));
		}
	} else if (operation_type == "wechat_login") { //微信登录
		$.get("http://127.0.0.1:8899/stu/wxLogin/tempUserId?secret=bd1bd4c7", function(res) {
			if (res.code == 0) {
				qrUrl = res.data.qrUrl;
				tempUserId = res.data.tempUserId;
			}
			$("#wechat_login_img").show().attr("src", qrUrl);
			$("#cancel_wechat_login").show();
			/* 指定时间访问后台判断是否登录 */
			wechat_interval = setInterval(function() {
				$.post("http://127.0.0.1:8899/api/consumer/UserController/we_chat_check_whether_login/" +
					tempUserId,
					function(data) {
						if (data.result == "we_chat_login_true") {
							location.href = "http://127.0.0.1:8899/html/" + (url_html_name != "" ?
								url_html_name : "car_index") + ".html" + (url_parameter != "" ?
								"?" + url_parameter : ""); // 默认回到首页
							clearInterval(wechat_interval);
						} else if (data.result == "we_chat_login_false") {
							$("#error-tips").text("登录失败");
							cancel_wechat_login();
							clearInterval(wechat_interval);
						} else if (data.result == "we_chat_on_line") {
							$("#error-tips").text("用户已在线，请稍后再试");
							cancel_wechat_login();
							clearInterval(wechat_interval);
						}
					}, "json");
			}, 1000);
			setTimeout(function() {
				clearInterval(wechat_interval);
			}, 59000);
		}, "JSON")
	} else if (operation_type == "register") { //注册
		if (check_phone() && check_verification_code() && check_user_password()) {
			send_ajax("http://127.0.0.1:8899/api/consumer/UserController/register", JSON.stringify({
				'user_phone': user_phone,
				'user_password': user_password
			}));
		}
	}
}

/* 发送请求 */
function send_ajax(url, data) {
	$.ajax({
		url: url,
		type: "post",
		data: data,
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			handle_button_submit(data);
		}
	});
}

/* 处理后台返回提交操作数据 */
function handle_button_submit(data) {
	$("#error-tips").text("");
	$("#error-tips").css("color", "red");
	$("#switch_tltle").css("color", "");
	if (data.result == "login_true") {
		location.href = "http://127.0.0.1:8899/html/" + (url_html_name != "" ? url_html_name : "car_index") + ".html" +
			(url_parameter != "" ? "?" + url_parameter : ""); //默认回到首页
	} else if (data.result == "login_false") {
		$("#error-tips").text("登录失败");
	} else if (data.result == "on_line") {
		$("#error-tips").text("用户已在线，请稍后再试");
	} else if (data.result == "register_true") {
		$("#error-tips").css("color", "green");
		$("#error-tips").text("注册成功，请登录");
		$("#switch_tltle").css("color", "green");
	} else if (data.result == "register_false") {
		$("#error-tips").text("注册失败，请稍后再试");
	} else if (data.result == "register_phone_true") {
		$("#error-tips").text("该手机号码已被注册，请重新输入");
	} else if (data.result == "token_false") { //token错误
		token = "";
		alert("登录错误，请稍后再试");
	}
	clear_all_mess(); //清空文本框
}

/* 验证手机号 */
function check_phone() {
	$("#error-tips").css("color", "red");
	var user_phone = $("[name='user_phone']").val();
	if (user_phone == null || user_phone.trim().length == 0) {
		$("#error-tips").text("手机号码不能为空");
		return false;
	}
	if (!/^1[3|4|5|7|8][0-9]{9}$/.test(user_phone)) {
		$("#error-tips").text("手机号码格式错误");
		return false;
	}
	$("#error-tips").text("");
	return true;
}

/* 验证密码 */
function check_user_password() {
	var user_password = $("[name='user_password']").val();
	if (user_password == null || user_password.trim().length == 0) {
		$("#error-tips").text("密码不能为空");
		return false;
	}
	if (user_password.trim().length < 6) {
		$("#error-tips").text("密码长度为6~10");
		return false;
	}
	$("#error-tips").text("");
	return true;
}

/* 验证验证码 */
function check_verification_code() {
	var verification_code = $("[name='verification_code']").val();
	if (verification_code == null || verification_code.trim().length == 0) {
		$("#error-tips").text("验证码不能为空");
		return false;
	}
	if (verification_code != true_verification_code) {
		$("#error-tips").text("验证码输入错误");
		return false;
	}
	if (check_count_down) {
		$("#error-tips").text("验证码已过期");
		return false;
	}
	$("#error-tips").text("");
	return true;
}

/* 获取验证码 */
function get_verification_code() {
	if (check_phone()) {
		$.post("http://127.0.0.1:8899/api/consumer/UserController/get_verification_code", function(data) {
			true_verification_code = data.verification_code;
			check_count_down = false;
			$("#sso_code").text("30s");
			$("#sso_code").css("color", "red");
			$("#sso_code").attr("onclick", "");
			interval = setInterval("downNumber()", 1000); //倒计时30秒
			setTimeout("stopDownNumber()", 30000);
		}, "json");
	}
}

/* 倒计时方法 */
function downNumber() {
	count_down--;
	$("#sso_code").text(count_down + "s");
}

/* 停止倒计时 */
function stopDownNumber() {
	window.clearInterval(interval);
	check_count_down = true; //验证码过期
	count_down = 30;
	$("#sso_code").text("获取验证码");
	$("#sso_code").css("color", "#1f2129");
	$("#sso_code").attr("onclick", "get_verification_code()");
}

/* 选中用户协议 */
function show_svg() {
	whether_check_svg = !whether_check_svg;
	if (whether_check_svg) { //选中
		$("#svg").show();
		$("#sso_submit").attr("onclick", "button_submit()");
		$("#sso_submit").css({
			"cursor": "pointer",
			"background-color": "#FFCC32"
		});
	} else {
		$("#svg").hide();
		$("#sso_submit").attr("onclick", "");
		$("#sso_submit").css({
			"cursor": "not-allowed",
			"background-color": "#F9F9F9"
		});
	}
}

/* 切换登录方式 */
function switch_login_type(operation_type_name) { //登录类型名称
	$("#cancel_wechat_login").hide();
	$("#wechat_login_img").hide();
	clear_all_mess(); //清空验证码，文本框
	operation_type = operation_type_name; //保存登录类型
	if (operation_type_name == "verification_code_login") { //切换为验证码登录
		$("#password_div").hide();
		$("#verification_code_div").show();
		$("#sso_pwd_login img:eq(0)").hide();
		$("#sso_pwd_login img:eq(1)").show();
		$("#login_title").text("验证码登录");
		$("#login_type").text("账密登录");
		$("#sso_pwd_login").attr("onclick", "switch_login_type('password_login')");
	} else if (operation_type_name == "password_login") { //切换为账密登录
		$("#password_div").show();
		$("#verification_code_div").hide();
		$("#sso_pwd_login img:eq(0)").show();
		$("#sso_pwd_login img:eq(1)").hide();
		$("#login_type").text("验证码登录");
		$("#login_title").text("账密登录");
		$("#sso_pwd_login").attr("onclick", "switch_login_type('verification_code_login')");
	} else if (operation_type_name == "wechat_login") { //切换为微信登录
		$("#sso_container").hide();
		button_submit();
	}
}

/* 取消微信登录 */
function cancel_wechat_login() {
	$("#sso_container").show();
	switch_login_type("verification_code_login");
}

/* 切换操作：注册/登录 */
function switch_register_or_login() {
	$("#cancel_wechat_login").hide();
	$("#wechat_login_img").hide();
	$("#error-tips").text("");
	$("#error-tips").css("color", "red");
	$("#switch_tltle").css("color", "");
	clear_all_mess(); //清空验证码，文本框
	whether_check_svg = true;
	show_svg(); //不选中用户协议
	/* 切换成注册 */
	if (operation_type != "register") { //登录
		$("#password_div").show();
		$("#switch_login").hide();
		$("#verification_code_div").show();
		operation_type = "register";
		$("#login_title").text("手机注册");
		$("#switch_tltle").text("立即登录");
		$("#sso_submit").val("注册");
		return;
	}
	$("#password_div").hide();
	$("#switch_login").show();
	$("#verification_code_div").show();
	$("#sso_pwd_login img:eq(0)").hide();
	$("#sso_pwd_login img:eq(1)").show();
	operation_type = "verification_code_login";
	$("#login_type").text("账密登录");
	$("#login_title").text("验证码登录");
	$("#switch_tltle").text("立即注册");
	$("#sso_submit").val("登录");
}

/* 清空验证码，文本框 */
function clear_all_mess() {
	check_count_down = true; //验证码过期
	count_down = 30;
	$("[name='user_phone']").val("");
	$("[name='verification_code']").val("");
	$("[name='user_password']").val("");
	stopDownNumber(); //停止倒计时
}
